﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Newtonsoft.Json;
using System.Data;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web.Person
{
    public partial class PersonCertStatistic : BasePage
    {
        protected string AreaCode
        {
            get
            {
                return UserInfo.CurrentArea;
            }
        }
        /// <summary>
        /// 作为查询地区限定条件
        /// </summary>
        protected string AreaCodeWhere
        {
            get
            {
                return UserInfo.AreaCode;
            }
        }
        /// <summary>
        /// 保存证书类型
        /// </summary>
        private Dictionary<string, string> dict
        {
            get
            {
                if (ViewState["dict"] != null)
                {
                    return (Dictionary<string, string>)ViewState["dict"];
                }
                return GetGetDictionary();
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //BindFlowCodes();
                //绑定列表
                rgPersonList.DataSource = ToDataTable();
            }
        }

        /// <summary>
        /// 取得审核状态绑定查询选项
        /// </summary>
        private void BindFlowCodes()
        {
            using (BasicService.BasicServiceClient client = new BasicService.BasicServiceClient())
            {
                string msg = string.Empty;
                var dic = JsonConvert.DeserializeObject<Dictionary<string, string>>(client.GetAllStatusFlowCodesByTitle(out msg, UserInfo.RoleId, AreaCode, ""));
                rblFlowCodeList.DataSource = dic;
                rblFlowCodeList.DataTextField = "key";
                rblFlowCodeList.DataValueField = "value";
                rblFlowCodeList.DataBind();
            }
        }

        /// <summary>
        /// 取得证书类型数据源
        /// </summary>
        /// <returns></returns>
        protected Dictionary<string, string> GetGetDictionary()
        {
            using (BasicService.BasicServiceClient client = new BasicService.BasicServiceClient())
            {
                var list = JsonConvert.DeserializeObject<List<T_Sys_Dict>>(client.GetSysDicts("ZGZSLC","0","14"));
                var dictTmp = list.ToDictionary(c => c.Code, c => c.Name);
                ViewState["dict"] = dictTmp;
                return dictTmp;
            }
        }

        /// <summary>
        /// 格式化结果集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rgPersonList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridHeaderItem)
            {
                ///隐藏地区号
                GridHeaderItem gridHearItem = e.Item as GridHeaderItem;
                gridHearItem["SortCode"].Visible = false;
            }
            int gridCount = 0;
            if (e.Item is GridDataItem)
            {
                GridDataItem gridItem = e.Item as GridDataItem;
                gridItem["SortCode"].Visible = false;
                ///根据证书类型列表取得相应列并将统计数目大于零的加链接
                foreach (var d in dict)
                {
                    if (gridItem[d.Value] != null)
                    {
                        int.TryParse(gridItem[d.Value].Text, out gridCount);
                        if (gridCount > 0)
                        {
                            gridItem[d.Value].Text = "<a href='../person/PersonEmpAuditList14.aspx?opt=1&areacode=" + gridItem["SortCode"].Text + "&groupId=" + d.Key + "&flowcode=" + Server.UrlEncode(rblFlowCodeList.SelectedValue) + "'><span style='color: #43a9f5'>" + gridCount.ToString() + "</span></a>";
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 审核状态查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rblFlowCodeList_SelectedIndexChanged(object sender, EventArgs e)
        {
            rgPersonList.DataSource = ToDataTable();
            rgPersonList.DataBind();
        }

        /// <summary>
        /// 取得数据源并将数据源转化成DataTable
        /// </summary>
        /// <returns></returns>
        public DataTable ToDataTable()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                //取得统计数据结果集
                var statist = JsonConvert.DeserializeObject<ModelService.StatisticsStruct>(client.GetPersonCertStatistic(rblFlowCodeList.SelectedValue.Trim(), AreaCode));
                //转化成DataTable
                DataTable result = UtilitiesTool.StructToDataTable(statist);
                if (result == null)
                {
                    return result;
                }
                //显示子地区统计数据
                if (AreaCodeWhere.Length > 2)
                {
                    return result.Select("SortCode='" + AreaCodeWhere + "'").CopyToDataTable();
                }
                return result;
            }
        }
    }
}